﻿using System;
using System.Data;
using System.Data.OracleClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class Show_MobileIndex : UserPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {            
            FlashBind();
            MoreUnLinkBind();           
            ReportBind();            
            Mtitle.InnerText = ConfigurationManager.AppSettings["SystemName"].Substring(0,6);
            MFoot.InnerText = DateTime.Now.Year.ToString() + "©"
                + ConfigurationManager.AppSettings["CompanyName"] + "Gt";            
        }
    }

    private void FlashBind()
    {
        string sqlStr = @"SELECT DISTINCT COUNTY,CELL_CHNAME,TO_NCHAR(CELL_CLASS) CELL_CLASS
                                  FROM AB_CELL_UNLINK 
                                 WHERE START_TIME > TO_CHAR(SYSDATE,'yyyy-MM-dd')
                                 GROUP BY COUNTY,CELL_CHNAME,CELL_CLASS,trunc(START_TIME,'HH24')
                                HAVING Count(1) >= 3
                                 UNION ALL
                                SELECT DISTINCT SUBSTR(T2.CELL_CHNAME,0,2) AS COUNTY,T2.CELL_CHNAME,T2.CELL_CLASS
                                  FROM ALARM_TABLE_3G T1
                                  LEFT JOIN CELL_CLASS T2
                                    ON T1.RNW_OBJECT_NAME = T2.CELL_ENNAME
                                 WHERE STARTED IS NOT NULL
                                   AND STARTED > TO_CHAR(SYSDATE,'yyyy-MM-dd')
                                   AND T2.CELL_CHNAME IS NOT NULL
                                   AND C_NUMBER = '7786'   
                                   AND TO_NUMBER((T1.CANCELED- T1.STARTED)*24*60) >= 1
                                   AND (TO_NUMBER(TO_CHAR(T1.STARTED,'HH24'))>5
                                    OR (NVL(TO_NUMBER(TO_CHAR(T1.CANCELED,'HH24')),0)>5 OR T1.CANCELED IS NULL))
                                 GROUP BY SUBSTR(T2.CELL_CHNAME,0,2),T2.CELL_CHNAME,T2.CELL_CLASS,trunc(T1.STARTED,'HH24')
                                HAVING COUNT(1) >= 3";
        DataSet ds = OracleHelper.ExecuteDataSet(sqlStr, CommandType.Text, null);
        if (ds.Tables[0].Rows.Count > 0)
        {
            FlashList.DataSource = ds;
            FlashList.DataBind();
            FlashList.Visible = true;
        }
    }

    private void MoreUnLinkBind()
    {
        string sqlStr = @"SELECT cell_chname,trunc((SYSDATE - START_TIME) * 24) as dual,TO_NCHAR(CELL_CLASS) CELL_CLASS
                                  FROM AB_CELL_UNLINK 
                                 WHERE (SYSDATE - START_TIME) * 24 >= 24
                                   AND (SYSDATE - START_TIME) * 24 <= 72
                                   AND RECOVER_TIME IS NULL
                                    UNION ALL
                                    SELECT T2.cell_chname,trunc((SYSDATE-STARTED)*24) as dual,T2.CELL_CLASS
                                  FROM ALARM_TABLE_3G T1
                                  LEFT JOIN CELL_CLASS T2
                                    ON T1.RNW_OBJECT_NAME = T2.CELL_ENNAME
                                 WHERE STARTED IS NOT NULL
                                   AND (SYSDATE-STARTED)*24>=24
                                   AND (SYSDATE-STARTED)*24<=72
                                   AND CANCELED IS NULL
                                   AND T2.CELL_CHNAME IS NOT NULL
                                   AND C_NUMBER = '7786'   
                                   AND (T1.CANCELED IS NULL OR TO_NUMBER((T1.CANCELED- T1.STARTED)*24*60) >= 1)
                                   AND (TO_NUMBER(TO_CHAR(T1.STARTED,'HH24'))>5
                                    OR (NVL(TO_NUMBER(TO_CHAR(T1.CANCELED,'HH24')),0)>5 OR T1.CANCELED IS NULL))
                                   ORDER BY dual DESC";
        DataSet ds = OracleHelper.ExecuteDataSet(sqlStr, CommandType.Text, null);
        if (ds.Tables[0].Rows.Count > 0)
        {
            MoreUnLink.DataSource = ds;
            MoreUnLink.DataBind();
            MoreUnLink.Visible = true;
        }
    }

    private DataSet Report_Data(string argType,DateTime max,string comp)
    {
        string sqlstr = "AB_GetInfo_Pk.GetUNLink";
        OracleParameter[] _params = {
                new OracleParameter("v_County",OracleType.VarChar,20),
				new OracleParameter("V_Begin",OracleType.VarChar,100),
				new OracleParameter("V_End",OracleType.VarChar,100),
				new OracleParameter("V_Type",OracleType.VarChar,100),
				new OracleParameter("resultSet",OracleType.Cursor)
            };
        _params[0].Value = comp;
        _params[1].Value = max.ToString("yyyy-MM-dd");
        _params[2].Value = max.AddDays(1).ToString("yyyy-MM-dd");
        _params[3].Value = argType;
        _params[4].Direction = ParameterDirection.Output;
        return OracleHelper.ExecuteDataSet(sqlstr, CommandType.StoredProcedure, _params);
    }

    private void ReportBind()
    {
        DateTime max = GetMaxDate();
        string comp = "";
        DataRow dr = GetOrganAndCompany().Rows[0];
        if ((!dr["company"].ToString().Equals("延安")) || dr["organ"].ToString().Equals("宝塔维护站"))
        {
            comp = dr["company"].ToString();
        }
        ReportA.DataSource = Report_Data("A类", max, comp).Tables[0].DefaultView;
        ReportA.DataBind();        
        ReportB.DataSource = Report_Data("B类", max, comp).Tables[0].DefaultView;
        ReportB.DataBind();        
        ReportC.DataSource = Report_Data("C类", max, comp).Tables[0].DefaultView;
        ReportC.DataBind();
    }

    private DateTime GetMaxDate()
    {
        string sqlstr = "select MAX(maxdate) as maxadte from AB_UPLOAD_LOG_T";
        string date = OracleHelper.ExecuteScalar(sqlstr, CommandType.Text, null).ToString();
        dateNew.InnerText = DateTime.Parse(date).ToString("yyyy-MM-dd 0点-HH:mm:ss");
        return DateTime.Parse(date);
    }

    protected void Modify_Click(object sender, EventArgs e)
    {
        FlashBind();
        MoreUnLinkBind();
        ReportBind();
    }
}
